package com.nsgf.library.ui.page;

import static android.view.View.VISIBLE;
import static com.nsgf.library.ui.product.ProductDetailActivity.setUpWebView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.data.api.APIs;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.event.EventMsg;
import com.nsgf.library.event.RxBus;
import com.nsgf.library.utils.Kits;
import com.nsgf.library.utils.StringUtil;
import com.nsgf.library.widget.prckerview.TimePickerView;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityNsgfWebviewBinding;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class WebViewApiActivity extends BaseActivity {


    private StateHolder mState;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    private ActivityNsgfWebviewBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_webview, BR.vm, mState);
    }

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityNsgfWebviewBinding) getBinding();
        mAccountRequester.getAgreement(getIntent().getStringExtra("id"));
        mAccountRequester.getLibraryInfo().observe(this, new Observer<ResponseStatus<LibraryInfo>>() {
            @Override
            public void onChanged(ResponseStatus<LibraryInfo> libraryInfoResponseStatus) {
                if (libraryInfoResponseStatus.getResponseCode() == 0) {
                    mBinding.tvCreateTime.setVisibility(VISIBLE);

                    try {
                        if (!StringUtil.isEmpty(libraryInfoResponseStatus.getData().getCreateTime())) {
                            Long createTime = Long.parseLong(libraryInfoResponseStatus.getData().getCreateTime());
                            mBinding.tvCreateTime.setText(Kits.Date.getYmdhms(createTime));
                        }
                        setUpWebView(mBinding.webView.getWebView(), libraryInfoResponseStatus.getData().getContent());
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        mBinding.titlebar.getRoot().setVisibility(VISIBLE);
        mState.titleBar.set(getIntent().getStringExtra("title"));
        WebSettings mWebSettings = mBinding.webView.getWebView().getSettings();
        mWebSettings.setJavaScriptEnabled(true);//设置支持javaScript
//
//
//        //获取当前html的title 并赋值给webview_title
//        mBinding.webView.getWebView().setWebChromeClient(new WebChromeClient() {
//            @Override
//            public void onReceivedTitle(WebView view, String title) {
//                // TODO Auto-generated method stub
//                super.onReceivedTitle(view, title);
//                mBinding.titlebar.titleBarRoot.setText(title);
//                //titles.add(title);
//            }
//        });


        // 当页面加载完成后调用JavaScript方法
        mBinding.webView.getWebView().setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }
        });


    }

    /**
     * 返回键返回上一网页
     */
    @Override
    public void onBackPressed() {
        if (mBinding.webView.canGoBack()) {
            mBinding.webView.goBack();
        } else {
            finish();
        }

    }
}
